package com.ex.boms.core.util.testfilereplace;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ex.boms.core.util.PropertiesUtil;

/**
 * 
 * @author smyun@ntels.com
 * 
 */
public class Exam05TreeFilesReplaceWithPropertiTest {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	private static final Logger slogger = LoggerFactory.getLogger(Exam01DirCopyTest.class);

	public static void main(String args[]) {
		new Exam05TreeFilesReplaceWithPropertiTest().runDirCopyTest();
	} // end function
	
	/**
	 * 폴더를 복사한다.
	 */
	public void runDirCopyTest() {
		Date today = new Date();
		
		//yyyy-MM-dd HH:mm:ss
		//String dateFormat = DateFormatUtils.format(today, "yyyyMMdd_HHmmss");
		String dateFormat = DateFormatUtils.format(today, "yyyyMMdd_HHmmss");
		
		String sourceFolder = "C:/eGovFrame.boms/workspace/BOMS/src/main/java/com/ex/boms/core/util/testfilereplace/temp1";
		String destinationFolder = sourceFolder + "_" + dateFormat;
		String file = "";
		File srcFile = new File(sourceFolder, file); // 기존 파일
		File destFile = new File(destinationFolder, file); // 복제 파일
		
		try {
			new Exam05TreeFilesReplaceWithPropertiTest().findFiles(srcFile, destFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	} // end function
	
	/**
	 * 폴더를 복사한다.
	 */
	public static void findFiles(File pfSourceFolder, File pfDestinationFolder) throws IOException {
		if (pfSourceFolder.isDirectory()) { // 디렉토리라면
			slogger.debug(pfSourceFolder.getAbsolutePath() + " [Dir]["+pfSourceFolder.list().length+"] "  + pfSourceFolder.getAbsolutePath());
			if (!pfDestinationFolder.exists()) { // 디렉토리가 존재하지 안다면
				pfDestinationFolder.mkdirs(); // 디렉토리생성
			}
			String files[] = pfSourceFolder.list(); // 파일목록
			for (String file : files) { // 파일목록 루프
				File srcFile = new File(pfSourceFolder, file); // 기존 파일
				File destFile = new File(pfDestinationFolder, file); // 복제 파일
				findFiles(srcFile, destFile); // 제외호출
			}
		} else { // 파일이면
			slogger.debug(pfSourceFolder.getAbsolutePath() + " [File] "  + pfSourceFolder.getAbsolutePath());
			copyFile(pfSourceFolder, pfDestinationFolder);
		}
	} // end function
	
	/**
	 * 파일을 복사한다.
	 */
	public static void copyFile(File pfSourceFile, File pfDestinationFile) throws IOException {
		slogger.debug(">>>>>>>>1 ["+pfSourceFile.getAbsolutePath()+"]");
		String sLineText = "";
		String sOldContents = "";
		String sNewContents = "";
		try {
			BufferedReader reader = new BufferedReader(new FileReader(pfSourceFile));
			while ((sLineText = reader.readLine()) != null) {
				sOldContents += sLineText + "\r\n";
			}
			reader.close();
			sNewContents = sOldContents;
			sNewContents = replace(sOldContents);
			FileWriter fwDestinationFile = new FileWriter(pfDestinationFile.getAbsoluteFile() + "_");
			fwDestinationFile.write(sNewContents);
			fwDestinationFile.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		
	} // end function
	
	private static String replace(String sContents){
		String  prop = "com/ex/boms/core/util/testfilereplace/Exam05TreeFilesReplaceWithPropertiTest.properties"; //  프로퍼티파일명
		Properties props = PropertiesUtil.get(prop) ;
		SortedMap smap = new TreeMap(props); 
		Set keySet = smap.keySet(); 
		Iterator iterator = keySet.iterator(); 
		String key = ""; 
		String value = "";
		String value1 = "";
		String value2 = "";
		while (iterator.hasNext()) { 
			key = "";
			value = "";
			value1 = "";
			value2 = "";
			key = (String) iterator.next(); 
			value = props.getProperty(key);
			String [] values = value.split("\\|");
			value1 = values[0];
			value2 = values[1];
			if(1 < values.length){
				value2 = values[1];
			}
			if(0 < value1.length()){
				sContents = sContents.replaceAll(value1, value2);
			}
		}
		return sContents;
	}
}

